package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.model.MarketAdmin;
import com.cskaoyan.model.MarketAdminExample;
import com.cskaoyan.service.AuthService;
import com.cskaoyan.utils.MyBatisUtil;
import com.cskaoyan.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author Pudding
 * @Date 2024/5/28 15:54
 */
public class AuthServiceImpl implements AuthService {
    @Override
    public MarketAdmin findByUsername(String username) {
        MarketAdminMapper adminMapper = TransactionUtil.getMapper(MarketAdminMapper.class);

        //MarketAdmin admin = adminMapper.selectByUsername(username);自己新增也可以，我们使用下面的example中方法
        MarketAdminExample example = new MarketAdminExample();
        example.createCriteria().andUsernameEqualTo(username)
                .andDeletedEqualTo(false);

        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);

        return (marketAdmins != null && marketAdmins.size() == 1) ? marketAdmins.get(0) : null;
    }

    @Override
    public MarketAdmin findById(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        MarketAdmin admin = adminMapper.selectByPrimaryKey(id);

        sqlSession.close();
        return admin;
    }
}
